import {
  reqGetCode,
  reqGetRegister,
  reqLogin,
  reqGetRegisterInfot,
  reqLoginout,
} from "@/api/index.js";

const state = {
  getCode: "",
  token: localStorage.getItem("Token"),
  getRegister: {},
};
const mutations = {
  GETCODE(state, getCode) {
    state.getCode = getCode;
  },
  GETLOGIN(state, token) {
    state.token = token;
  },
  GETREGISTER(state, getRegister) {
    state.getRegister = getRegister;
  },
  LOGINOUT(state) {
    state.token = "";
    state.getRegister = {};
    localStorage.removeItem("Token");
  },
};
const actions = {
  async getCode({ commit }, phone) {
    let result = await reqGetCode(phone);
    if (result.code == 200) {
      commit("GETCODE", result.data);
      return "ok";
    } else {
      return Promise.reject(new Error("faile"));
    }
  },
  async getRegister({ commit }, use) {
    let result = await reqGetRegister(use);
    if (result.code == 200) {
      return "ok";
    } else {
      return Promise.reject(new Error("faile"));
    }
  },
  async getLogin({ commit }, data) {
    let result = await reqLogin(data);

    if (result.code == 200) {
      commit("GETLOGIN", result.data.token);
      localStorage.setItem("Token", result.data.token);

      return "ok";
    } else {
      return Promise.reject(new Error("faile"));
    }
  },
  async getUserInfo({ commit }) {
    let result = await reqGetRegisterInfot();
    if (result.code == 200) {
      commit("GETREGISTER", result.data);
    }
  },
  async getLoginout({ commit }) {
    let result = await reqLoginout();
    console.log(result);
    commit("LOGINOUT");
  },
};
const getters = {};

export default {
  state,
  mutations,
  actions,
  getters,
};
